<?php
class CtrinhsController extends AppController {

	var $name = 'Ctrinhs';
	var $components = array('Auth');
	var $uses=array('Ctrinh');

	function index() {
	
		$this->Ctrinh->recursive = 0;
		$this->set('ctrinhs', $this->paginate('Ctrinh'));
	}
	

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid ctrinh', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('ctrinh', $this->Ctrinh->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Ctrinh->create();
			if ($this->Ctrinh->save($this->data)) {
				$this->Session->setFlash(__('The ctrinh has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Dữ liệu được lưu, vui lòng thử lại', true));
			}
		}
		$chudauts = $this->Ctrinh->Chudaut->find('list',
			array('fields'=>array('id','tenchudautu')));
	//	print_r($chudauts);
		$dvthicongs = $this->Ctrinh->Dvthicong->find('list',
			array('fields'=>array('id','tendvthicong')));
		$hthucdauts = $this->Ctrinh->Hthucdaut->find('list',
			array('fields'=>array('id','tenht')));
	//	$hangmucs = $this->Ctrinh->Hangmuc->find('list',
	//		array('fields'=>array('id','tenhm')));
	
		$this->set(compact('chudauts', 'dvthicongs', 'hthucdauts', 'hangmucs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid ctrinh', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Ctrinh->save($this->data)) {
				$this->Session->setFlash(__('The ctrinh has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Dữ liệu được lưu, vui lòng thử lại', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Ctrinh->read(null, $id);
		}
		$chudauts = $this->Ctrinh->Chudaut->find('list',
			array('fields'=>array('id','tenchudautu')));
		$dvthicongs = $this->Ctrinh->Dvthicong->find('list',
			array('fields'=>array('id','tendvthicong')));
		$hthucdauts = $this->Ctrinh->Hthucdaut->find('list',
			array('fields'=>array('id','tenht')));
	//	$hangmucs = $this->Ctrinh->Hangmuc->find('list',
	//		array('fields'=>array('id','tenhm')));
		$this->set(compact('chudauts', 'dvthicongs', 'hthucdauts', 'hangmucs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for ctrinh', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Ctrinh->delete($id)) {
			$this->Session->setFlash(__('Ctrinh deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Ctrinh was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
